package com.google.project.client.ProjectManager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.FormElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.project.client.Service.UploadService;
import com.google.project.client.Service.UploadServiceAsync;

public class UploadDialogView extends DialogBox {

	private final UploadServiceAsync uploadService = GWT.create(UploadService.class);
	
	private static final Binder binder = GWT.create(Binder.class);
	private final String forID;
	private final String dataType;
	
	String dataType2 ;
	private final String projectName;
	private final String taskName;
	@UiField FormPanel formUpload;
	@UiField TextBox txtDocName;
	@UiField TextArea txtDescription;
	@UiField FileUpload fileUpload;
	@UiField Button btnUpload;
	@UiField Hidden forId;
	@UiField Hidden type;
	@UiField Button button;

	interface Binder extends UiBinder<Widget, UploadDialogView> {
	}

	public UploadDialogView(String forID, String dataType, String projectName, String taskName) {
		setWidget(binder.createAndBindUi(this));
		this.forID = forID;
		dataType2 = dataType;
		if(dataType.equals("21"))
		{
			this.dataType = "2";
		}
		else
		{
			this.dataType = dataType;
		}
		
		this.projectName = projectName;
		this.taskName = taskName;
		forId.setDefaultValue(forID);
		type.setDefaultValue(this.dataType);
	}
	
	@UiHandler("btnUpload")
	void onBtnUploadClick(ClickEvent event) {
		uploadService.getBlobStoreUrl(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				formUpload.setEncoding(FormPanel.ENCODING_MULTIPART);
				formUpload.setMethod(FormPanel.METHOD_POST);
				FormElement.as(formUpload.getElement()).setAcceptCharset("UTF-8");
				formUpload.setAction(result.toString());
				formUpload.submit();
				formUpload.reset();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}
		});
		
		formUpload.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				closeDialog();
				PMEntrypoint.get().ListDocuments(forID, dataType2, projectName, taskName);
			}
		});
	}
	@UiHandler("button")
	void onButtonClick(ClickEvent event) {
		closeDialog();
	}
	
	public void closeDialog(){
		this.hide();
	}
}
